package org.xy.yq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xy.yq.entity.User;
import org.xy.yq.service.IUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Qualifier("userServiceImpl")
    @Autowired
    private IUserService userService;
    @RequestMapping("/login")
    public String userLogin(String username, String password, HttpSession session){
        User u = userService.login(username,password);
        String path="";
        List<User> users = userService.selectAll();
        System.out.println(u.getUsername()+".....");
        if(u!=null){
            session.setAttribute("users",users);
            session.setAttribute("user",u);
            path="redirect:/user/index/success";//重定向
        }else{
            System.out.println("用户名或者密码错误");
            path="login";//转发
        }
        return path;
    }
    @RequestMapping("/index/success")
    public String userIndex(){
        System.out.println("访问我之前必须要进行登录");
        return "index";
    }
    @RequestMapping("/register")
    public String register(User user){
        boolean flag = userService.register(user);
        String path="";
        if(flag){
            path="redirect:/index/login";
        }else{
            path="register";
        }
        return path;
    }
    @RequestMapping("/index/table")
    public String table(){
        return "table_jqgrid";
    }
    @ResponseBody
    @RequestMapping(value = "/index/selectAll",produces = "application/json;charset=UTF-8")
    public List<User> tableList(){
        List<User> users = userService.selectAll();
        return users;
    }
    @RequestMapping(value = "/index/tableEdit")
    public String tableEdit(HttpServletRequest request,User user){
        String md=request.getParameter("oper");
        String path="";
        if("add".equals(md)){
            path = add(user);
        }
        if("edit".equals(md)){
            path = edit(user);
        }
        if("del".equals(md)){
            int id = Integer.parseInt(request.getParameter("id"));
            path = del(id);
        }
        return path;
    }
    public String add(User user){
       boolean flag =  userService.register(user);
       String path="";
       if(flag){
           path="index";
       }
       return path;
    }
    public String edit(User user){
        boolean flag =  userService.updateUser(user);
        String path="";
        if(flag){
            path="index";
        }
        return path;
    }
    public String del(int id){
        boolean flag = userService.deleteById(id);
        String path="";
        if(flag){
            path="index";
        }
        return path;
    }

}
